package com.atguigu.gulimall.order.config;


import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;


//远程请求调用丢失请求头的问题

@Configuration
public class GuliFeignRequestIntercepter {

    @Bean("requestInterceptor")
    public RequestInterceptor requestInterceptor() {
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate template) {
                //RequestContextHolder拿到刚进来的请求
                ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
                        .currentRequestAttributes();
                HttpServletRequest request = attributes.getRequest();
                //同步请求头的数据，Cookie
                String cookie = request.getHeader("Cookie");
                //将老请求的登录信息给新的请求
                template.header("Cookie", cookie);
            }
        };
    }


}
